<?
/** @file
 * @author Andrzej Mararok Wasiak (mararok@gmail.com)
 */
   
error_reporting(-1);

/** Base for loggers
 */
class Logger {
   const INFO = 0; // informacja
   const WARNING = 1; // ostrzeżenie
   const EXCEPTION = 2; // wyjątek
   const SQL_ERROR = 3; // błąd w zapytaniu SQL
   const FATAL_ERROR = 4; // fatalny błąd
   
   private $Messages = array();
   private $LogTable;
   function __construct($logtable) {
      $this->LogTable = $logtable;
   }
   
   function __destruct() {
      $this->saveAll();
   }
   
   function log($type, $msg, $user = 0) {
      if ($type == self::SQL_ERROR) {
         $msg = mysql_real_escape_string($msg);
         $this->saveDirect($type,$msg,$user);
      } else {
         array_push($this->Messages,array($type,(int)$user,$msg));
      }
   }
   
   function saveDirect($type, $msg, $user) {
      $now = date("Y-m-d H:i:s"); 
      $q = 'INSERT INTO '.$this->LogTable.' (date,type,user,msg) VALUES '."('$now',$type,$user,'$msg' )";
      DB::query($q); 
   }
   
   function saveAll() {
      if (count($this->Messages) == 0) return;
      $now = date("Y-m-d H:i:s"); 
      $q = 'INSERT INTO '.$this->LogTable.' (date,type,user,msg) VALUES ';
      foreach ($this->Messages as &$msg) {
         $q .= "('$now',$msg[0],$msg[1], '$msg[2]' ),";
      }
      unset($msg);
      $q = trim($q,',');
      
      DB::query($q);    
   }

}

?>